<?php #title=Тест;
	
	showNotExistImages();
	
	
/* 	
	echo 'Код;Префикс;Бренд;Модель;Цена;CBID;BID;ПП;ПМ;Ссылка<br/>';
	$conn->query('SELECT * FROM store_products_cache');
	while( $row = $conn->fetch() )
	{
		echo $row->product_code.';'.$row->prefix.';'.$row->brand.';'.$row->name.';'.$row->price_retail.';'.$row->cbid.';'.$row->bid.';'.$row->pp.';'.$row->pm.';http://raybt.ru/'.getProductBeforeUrl( $row->parents ).$row->url.'<br/>';
	}
	 */
	
	
	

/* 	$conn->query('SELECT * FROM xml_price_update_times');
	while( $row = $conn->fetch() )
		$elog->add( 'Автоматическая генерация прайса: '.date( 'Y-m-d-H-i-s', $row->update_time ) );
	 */


/* 	if( 6 % 2 )
		echo 'нечетное';
	else
		echo 'четное'; */


/* 

	$view->js_on_load[] = '
		$(document).ready(function(){
			jQuery(".carousel-demo1").sliderkit({
				auto:false,
				shownavitems:4,
				circular:true
			});
		});';
			
			
	$view->gText .= '
	
		<div class="sliderkit carousel-demo1">
		<div class="sliderkit-nav">
			<div class="sliderkit-nav-clip">
				<ul>
					<li><a href="#"><img src="../inc/images/temp/sample-01.jpg"/></a></li>
					<li><a href="#"><img src="../inc/images/temp/sample-02.jpg"/></a></li>
					<li><a href="#"><img src="../inc/images/temp/sample-03.jpg"/></a></li>
					<li><a href="#"><img src="../inc/images/temp/sample-04.jpg"/></a></li>
					<li><a href="#"><img src="../inc/images/temp/sample-05.jpg"/></a></li>
					<li><a href="#"><img src="../inc/images/temp/sample-06.jpg"/></a></li>
					<li><a href="#"><img src="../inc/images/temp/sample-07.jpg"/></a></li>
				</ul>
			</div>
			<div class="sliderkit-btn sliderkit-nav-btn sliderkit-nav-prev"><a href="#" title="Previous line"><span>Previous</span></a></div>
			<div class="sliderkit-btn sliderkit-nav-btn sliderkit-nav-next"><a href="#" title="Next line"><span>Next</span></a></div>
		</div>
	</div>
	
	
	';


 */
/* 
		$conn->query('SELECT * FROM store_product_brand');
		if( isset( $_FILES['file']["error"] ) && $_FILES['file']["error"] == 0 )
		{
			$file = fopen( $_FILES['file']['tmp_name'] , 'r' );  // Читаем файл
			while( ( $data = fgetcsv( $file , '' , ',' , '"' ) ) !== FALSE )
			{
				 $data = array_map( 'trim', array_map( 'iconvWU', $data ) );
				 if( $data[2] )
				 {
					$conn->query('UPDATE store_product_brand SET name_rus = "'.mysql_real_escape_string( $data[2] ).'" WHERE name = "'.mysql_real_escape_string( $data[1] ).'"' );
				 }
			}
			fclose( $file );
		}

		$view->gText .= '
			<form ENCTYPE="multipart/form-data" action="" method="POST">
				<input type="file" name="file"/>
				<input type="submit"/>
			</form>';
		 */
/* 		$c = 0;
		$conn->query('SELECT pp, COUNT(pp) AS count_pp FROM store_products_cache GROUP BY pp ORDER BY pp');
		while( $row = $conn->fetch() )
		{
			$r[$row->pp] = $row->count_pp;
			if( in_array( $row->pp , array( 0, 1 ) ) )
			{
				$statuses[$row->pp] = $row->count_pp;
			}
			elseif( in_array( $row->pp, array( 3, 4, 5 ) ) )
			{
				$c += $row->count_pp;
				$statuses[3] = $c;
			}
		}
		
	echo '<pre>';
	print_r( $r );
	print_r( $statuses );
	 */

	
	
	// $conn->query('show create database as fdd raybt_b');
	// $row = $conn->fetch();
	// echo $row->fdd;
	
/* 
	echo time().'<br>';
	echo filemtime( '../inc/images/products/1001170.jpg' );
	echo '<br>';
	echo (int)( ( time() - filemtime( '../inc/images/products/1001170.jpg' ) ) / 24 / 60 / 60 );
	 */
	 

/* 	 
	$path = PATH_TO_FILES.'/'.PATH_TO_ARCHIVE_XML_DIR;
	// $path = PATH_TO_FILES.'/'.PATH_TO_ARCHIVE_CSV_DIR;
	$dir = opendir( $path );
	while( $file_name = readdir( $dir ) )
	{
		if( $file_name == '.' || $file_name == '..' || is_dir( $path.'/'.$file_name ) )
			continue;
		
		$file = $path.'/'.$file_name;
		echo getNumberDaysFromCreationFile( $file ).'<br>';
		
		
		// $elog->add( $i++.' Удалено '.$file_name );
		
	}
		
	function getNumberDaysFromCreationFile( $file )
	{
		$days = 0;
		if( is_file( $file ) ) {
			$now_time = time();
			$file_time = filemtime( $file );
			$days = intval( ( $now_time - $file_time ) / 24 / 60 / 60 );
		}
		return $days;
	}

	 */
	
	//if(  )
	
/* 
		// Удаление лишних картинок
	$conn->query('SELECT `product_code` FROM `store_products_view`');
	while( $row = $conn->fetch() )
		$products[] = $row->product_code;
	
	$time = time();
	$i = 1;
	$path = '../inc/images/products';  // Папка с картинками товаров
	$dir = opendir( $path );
	while( $file_name = readdir( $dir ) )
	{
		if( $file_name == '.' || $file_name == '..' || is_dir( $path.'/'.$file_name ) )
			continue;
				
		if( ( time() - $time ) > 5 )
			break;
		
		if( !in_array( getFileName( $file_name ), $products ) ) {
			unlink( $path.'/'.$file_name );
			usleep( 100 );
			$elog->add( $i++.' Удалено '.$file_name );
		}
	}
	
	function getFileName( $file ) {
		return substr( $file, 0, strrpos( $file, '.' ) );
	}
	 */
/* 	$path = '../inc/images/products';  // Папка с картинками товаров
	$conn->query('SELECT * FROM `store_products_view`');
	while( $row = $conn->fetch() )
	{
		$file = $path.'/'.$row->product_code.'.jpg';
		if( is_file( $file ) )
		{
			unlink( $file );
			$elog->add( 'Удалено: '.$row->product_code.'.jpg' );
		}
	} */
	
	
	
/* 	echo getXmlDataId( 'Вертолет' );
	echo getXmlDataId( 'Самолет' );
	
	function getXmlDataId( $val )
	{
		global $conn;
		static $first = true;
		static $store = array();
		
		if( $first )
		{
			$conn->query('SELECT * FROM store_product_xml_data WHERE type = "sales_notes"');
			while( $row = $conn->fetch() )
				$store[$row->value] = $row->id;			
			$first = false;
		}
		
		if( isset( $store[$val] ) )
			return $store[$val];			
		
		$id = $conn->query('INSERT INTO store_product_xml_data SET value = "'.mysql_real_escape_string( $val ).'", type = "sales_notes"', true);
		$store[$val] = $id;
		return $id;
	}
		 */
/* 	
	$mask = '../inc/files/descriptions'; // маска имён файлов
	$dir = opendir( $mask );
	while( $res = readdir( $dir ) )
	{
		if( $res == '.' || $res == '..' )
			continue;
		
		$file = $mask.'/'.$res;
		$cont = file_get_contents( $file );
		$cont = iconv('UTF-8', 'WINDOWS-1251', $cont );
		file_put_contents( $file, $cont );
		$elog->add( $res );
	}
	 */
/* 	function fuck( $str )
	{
		$char = 'ASCII';
		for( $i = 0; $i < 3; $i++ )
		{
			$ch = substr( $str, $i, 1 );
			$ord = ord( $ch );
		}

	} */
	
	
	/* 
	$files = glob($mask);
	foreach ( $files as $file ) {
		if ( !is_file( $file ) ) continue;
		$fp = fopen( $file, 'r+' );
		$data = fread( $fp, filesize($file) );
		rewind( $fp );
		ftruncate( $fp, 0 );
		fwrite($fp, iconv('UTF-8', 'WINDOWS-1251', $data));
		fclose($fp);
	}
 */

	
	set_time_limit(0);
	// error_reporting( 0 );
	

	
/* // 	$func = 'check_';
	$name = 'int';

	// $func.$name( $name );
	
	$funcName = 'check_'.$name;
	$funcName( $name );
	
	function check_int( $name )
	{
		echo $name;
	} */
/* 
 	$conn->query('SELECT bp.page_id, p.product_code FROM `store_basket_products` AS bp	INNER JOIN store_products_view AS p ON bp.page_id = p.page_id', 0, 1 );
	while( $row = $conn->fetch(1) )
	{
		$conn->query('UPDATE `store_basket_products` SET `product_code` = "'.$row->product_code.'" WHERE `page_id` = '.$row->page_id );
		$elog->add( $row->product_code );
	}
 */
/* 
	$conn->query('SELECT * FROM `store_product_brand`', 0, 1 );
	while( $row = $conn->fetch(1) )
	{
		$conn->query('UPDATE `store_product_brand` SET `url` = "'.getGoodUrl( $row->name ).'" WHERE `id` = '.$row->id );
		$elog->add( $row->name );
	}
 */
	
/* 	
	$start = (int)( memory_get_usage() / 1024 );
	
	
		if( isset( $_FILES['import_product']["error"] ) && $_FILES['import_product']["error"] == 0 )
		{
			$file = fopen( $_FILES['import_product']['tmp_name'] , 'r' );  // Читаем файл
			while( ( $data = fgetcsv( $file , '' , ';' , '"' ) ) !== FALSE )
			{
				$d[] = $data;
			}
			fclose( $file );
			unset( $d );
		}
		
	$end =	(int)( memory_get_usage() / 1024 );
	
	$elog->add( getPriceFormat( $end - $start ) );
	
	$view->gText .= '
					<form  ENCTYPE="multipart/form-data" action="" method="POST">
						<input type="file" name="import_product">
						<input type="submit" name="import_product" value="Отправить файл" />
					</form>';
	 */
	
/*  
	$a = 0;
	$b = 0;
	
	if( !$a OR !$b )
		echo 'истина';
	else
		echo 'ложь';
 */
/* 
	$arr = $main_cat_ids = array();
	
	$conn->query( '	SELECT id, templ, parents, title FROM pages WHERE templ IN( "category", "sub_category" )');
	while( $row = $conn->fetch() )
		$navi[$row->templ][( $row->parents ? $row->parents : 'n' )][] = $row;

	foreach( $navi['category']['n'] as $main_cat )
	{
		$main_cat_ids[trim( $main_cat->title )] = $main_cat->id;  // Уцененные товары
		$arr[$main_cat->title][] = $main_cat->parents.'#'.$main_cat->id.'#';
		
		if( isset( $navi['sub_category']['#'.$main_cat->id.'#'] ) )
			foreach( $navi['sub_category']['#'.$main_cat->id.'#'] as $cat )
			{

				$arr[$main_cat->title][$cat->title] = $cat->parents.'#'.$cat->id.'#';
				
				if( isset( $navi['sub_category']['#'.$main_cat->id.'##'.$cat->id.'#'] ) )
					foreach( $navi['sub_category']['#'.$main_cat->id.'##'.$cat->id.'#'] as $sub_cat )
						$arr[$main_cat->title][$sub_cat->title] = $sub_cat->parents.'#'.$sub_cat->id.'#';
			}
	}
	echo '<pre>'; print_r( $main_cat_ids );
  */
	
	
/* 	$ids = getIdsArr( '#3##19##65#' );
	
	echo '<pre>'; print_r( $ids );
	array_shift( $ids );
	echo '<pre>'; print_r( $ids );
	
	$conn->query('SELECT `id`, `parents`, `title`, `templ` FROM `pages` WHERE `templ` = IN( "category", "sub_category" ) ');
	while( $row = $conn->fetch() )
	{
		if( $row->templ == 'category' )
			$main_cats[$row->id]	= $row->title;
		else
			$cats[$row->id]			= $row->title;
	}
	 */
	
	// echo $conn->query('INSERT INTO store_p_charact_fields SET name = "Защита 2", name_sys = "s"
	// 				ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID( id ), name = "Защита 2" ', true);
	// $row = $conn->fetch();
	// echo $row->id;
	
	
/* 	$count = array();
	
	foreach( array( 1,2,3,4,5,4,3,2,1,5,4,3,2,5,4,5,3,4,2,3,4,6,6,6,6,6,6, ) as $n )
	{
		$count[$n] = 'on';
	}
	echo '<pre>';
	print_r( $count ); */
	// count_updated_products[$code];
	
	
	// renameDirAndFile( '../inc/descriptions' );
/* 	mb_internal_encoding('utf-8');
	$elog->add(file_get_contents( '../inc/descriptions/1000019.html' ));
	$elog->add(mb_detect_encoding( file_get_contents( '../inc/descriptions/1000019.html' ) ));
	$elog->add(file_get_contents( '../inc/descriptions/1001255.html' ));
	$elog->add(mb_detect_encoding( file_get_contents( '../inc/descriptions/1001255.html' ) ));
	
	
	function renameDirAndFile( $patch )
	{
		$handle = opendir( $patch );
		while( $file = readdir( $handle ) )
		{
			if( is_file( $patch.'/'.$file ) )
			{
				$f = file_get_contents( $patch.'/'.$file );
				echo $patch.'/'.$file;
				// echo mb_detect_order( 'ASCII' );
				// $f = iconv( 'ASCII', 'CP1251', $f );
				break;
				// $f = charset_x_win( $f ) ;
				
				file_put_contents( $patch.'/'.$file, $f );
			}
		}
		closedir( $handle );
	} */



	
	
	
	// usleep(100000);
	// echo 'Привет!';
	
	// updateIndexesTableProducts();  // array( 1002019, 2001768, 2003670 )
	
	// getImagesFromHappy();
	
	
	// updatePm();
	
	function updatePm()
	{
		global $conn;
		$conn->query('SELECT `product_code`, `pd` FROM `store_products_view` WHERE `pd` = 1', 0, 1 );
		while( $row = $conn->fetch(1) )
			$conn->query('UPDATE `store_products_view` SET `pm` = '.$row->pd.' WHERE `product_code` = '.$row->product_code );
	}
	
	
	
	function showNotExistImages()
	{
		global $conn, $elog;
		$conn->query('SELECT `product_code`, `name` FROM `store_products_view` ORDER BY `product_code`');
		echo 'СПИСОК КОДОВ ТОВАРОВ БЕЗ КАРТИНОК:' ;
		while( $product = $conn->fetch() )
		{
			if( !file_exists( '../inc/images/products/'.$product->product_code.'.jpg' ) )
				echo $product->product_code.' '.$product->name.'<br/>';
		}
	}
	
	function  getImagesFromHappy()
	{
		global $conn, $elog;
		$success = $failure = array();
		
		$time = time();
		$i = 1;
		$conn->query('SELECT `product_code` FROM `store_products_cache` ORDER BY `product_code`');
		while( $product = $conn->fetch() )
		{
			
			
			$file = 'http://happystore.ru/published/publicdata/HSBASE/attachments/SC/products_pictures/'.$product->product_code.'_enl.jpg';
			$newfile = '../inc/images/products/'.$product->product_code.'.jpg';

			if( !file_exists( $newfile ) )
			{
				if( copy( $file, $newfile ) )
					$success[] = $product->product_code;
				else
					$failure[$product->product_code] =  $file;
				usleep( 100000 );
			}

			if( ( time() - $time ) > 58 )
				break;
			$i++;
		}
		
		$elog->add( 'количесиво проверенных товаров '.$i );
		
		foreach( $failure as $code => $url )
			$elog->add( 'не удалось скопировать <a href="'.$url.'">'.$code.'.jpg</a>' );

		foreach( $success as $succ )
			$elog->add( 'удачно скопировался '.$succ.'.jpg' );
	}